//
//  AddRoomVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/7.
//  Copyright © 2020 tym. All rights reserved.
//

#import "AddRoomVC.h"
#import "AddRoomCell.h"
#import "UIView+ZFFrame.h"
#import "UIButton+Gradient.h"
#import "Sizes.h"
#import "IQKeyboardManager.h"
#import "FamilyService.h"
#import "FamilyRoomService.h"
#import "GroupShadowTableView.h"
@interface AddRoomVC ()<GroupShadowTableViewDelegate,GroupShadowTableViewDataSource>

@property(nonatomic,weak) IBOutlet UIView  *contView;
@property(nonatomic,weak) IBOutlet UITextField *nameFid;
@property(nonatomic,weak) IBOutlet UIButton *saveBtn;

//@property(nonatomic,strong)  GroupShadowTableView *tableView;

@property(nonatomic,strong) FamilyRoomService *roomService;
@property(nonatomic,strong) FamilyService *familyService;
@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) NSMutableArray *seleDataArray;
@end

@implementation AddRoomVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"添加房间"];
    //[self setTable];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    
    [self.saveBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    self.saveBtn.layer.cornerRadius = 5;
    self.saveBtn.layer.masksToBounds = YES;
    
   // [self getFamilyUserList];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(NSMutableArray*)seleDataArray{
    if (_seleDataArray == nil) {
        _seleDataArray = [[NSMutableArray alloc] init];
    }
    return _seleDataArray;
}

//-(void)getFamilyUserList{
//
//    [self.familyService getFamilyuserfindfamilyusers:[UserTool getCurrentFamily].familyId status:1 type:2 completion:^(NSError * _Nonnull error, NSArray<UserModel *> * _Nonnull array) {
//        [self.dataArray addObjectsFromArray:array];
//        [self.tableView reloadData];
//    }];
//}

-(FamilyRoomService*)roomService{
    if (_roomService == nil) {
        _roomService = [[FamilyRoomService alloc] init];
    }
    return _roomService;
}

-(FamilyService*)familyService{
    if (_familyService == nil) {
        _familyService = [[FamilyService alloc] init];
    }
    return _familyService;
}

//- (void)setTable {
//    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 51+13, ScreenWidth, ScreenHeight-NavigateonHeight-74-TabbarHeight-60) style:UITableViewStyleGrouped];
//    self.tableView.groupShadowDelegate = self;
//    self.tableView.groupShadowDataSource = self;
//    self.tableView.backgroundColor = viewBGColor;
//    self.tableView.headTitles = @[@"添加该房间成员"];
//    self.tableView.sectionFooterHeight = 0;
//    _tableView.showSeparator = NO;
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [self.view addSubview:_tableView];
//}

-(IBAction)getCreateRoom:(id)sender{
    if (self.nameFid.text.length == 0) {
        
        return;
    }
    TYMWeak(self);
    [self.roomService getFamilyroomcreatefamilyroomFamilyId:[UserTool getCurrentFamily].familyId roomName:self.nameFid.text roomDesc:@"" image:@"" rooID:0 completion:^(LMJBaseResponse * _Nonnull response) {
        if (!response.error) {
            [WHToast showMessage:@"房间创建成功" duration:2 finishHandler:^{
                for (UserModel *model in weakself.dataArray) {
                    if (model.isSelected) {
                        [weakself.seleDataArray addObject:[NSString stringWithFormat:@"%ld",model.userId]];
                    }
                }
                if (weakself.seleDataArray.count) {
                    NSInteger roomID = [response.responseObject[@"data"] integerValue];
                    [weakself addUserWithRoomID:roomID];
                }else{
                   [weakself.navigationController popViewControllerAnimated:YES];
                }
            }];
        }else{
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }
    }];
}

//向房间中添加成员
-(void)addUserWithRoomID:(NSInteger)roomID{
    TYMWeak(self);
    [self.roomService getCreatefamilyroomuserFamilyId:[UserTool getCurrentFamily].familyId familyRoomId:roomID userIdList:self.seleDataArray completion:^(LMJBaseResponse * _Nonnull response) {
        if (!response.error) {
            [weakself.navigationController popViewControllerAnimated:YES];
        }else{
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }
    }];
}

//#pragma mark delegate datasource
//- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
//    return 1;
//}
//
//- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.dataArray.count;
//}

//- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == self.dataArray.count-1) {
//        return 55;
//    }
//    return 45;
//}
//
//- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    AddRoomCell *cell = [tableView dequeueReusableCellWithIdentifier:[AddRoomCell identifier]];
//    if (!cell) {
//        cell = [[AddRoomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[AddRoomCell identifier]];
//    }
//    [cell setUserModel:self.dataArray[indexPath.row]];
//    return cell;
//}

//-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    UserModel *model = self.dataArray[indexPath.row];
//    model.isSelected = !model.isSelected;
//   // [self.tableView reloadData];
//}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![_nameFid isExclusiveTouch]) {
        [_nameFid resignFirstResponder];
    }
}

@end
